package com.dw.common;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadFactory {
    private static Executor executor;
    private static ScheduledExecutorService scheduledExecutorService;

    public static Executor getExecutor() {
        if (executor == null) {
            synchronized (ThreadFactory.class) {
                if (executor == null) {
                    executor = new ThreadPoolExecutor(4, 8, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
                }
            }
        }
        return executor;
    }

    public static ScheduledExecutorService getScheduledExecutor() {
        if (scheduledExecutorService == null) {
            synchronized (ThreadFactory.class) {
                if (scheduledExecutorService == null) {
                    scheduledExecutorService = new ScheduledThreadPoolExecutor(2, new BasicThreadFactory.
                            Builder().namingPattern("check-not-required-%d").daemon(true).build());
                }
            }
        }
        return scheduledExecutorService;
    }
}
